package FE;

import org.omg.CORBA.BooleanHolder;
import org.omg.CORBA.StringHolder;

import common.CommonTools;
import common.Marshall;
import common.UDPTools_1;
import frontend.FrontEndPOA;



public class FEImpl extends FrontEndPOA {
	
	UDPTools_1 uDPTools_1; 
	public FEImpl(){
		uDPTools_1 = new UDPTools_1();		
	}
	@Override
	public void createPlayerAccount(String firstName, String lastName,
									short age, String username, String password, String iPAddress,
									BooleanHolder result, StringHolder status) {
		

		//System.out.println("start new call for createPlayerAccount  username: "+ username );

		CommonTools.writeLog("frontend", "frontend",
							 "start new call for createPlayerAccount  username: "
						    + username 
						    , "frontend");
		
		// marshall the CORBA call to a string;
		String message = buildCreatePlayerAccountMsg(firstName, lastName, (int)age, username, password, iPAddress);		

		String responds = uDPTools_1.sendReceiveUDP(Integer.parseInt(CommonTools.getproperty("frontend.properties","UDPPort_" + "replica_0")),
													CommonTools.getproperty("frontend.properties", "serverName_" + "replica_0"),
													"replica_0",
													"frontend", 
													message);

		CommonTools.writeLog("frontend", "frontend",
				 "We got a response from the leader: "
			    + responds 
			    , "frontend");
		
		
		if(responds.equals("true")) {
			result.value = true; 
			status.value = "player creation successeded";
		} else{
			result.value = false; 
			status.value = responds;
		}
		CommonTools.writeLog("frontend", "frontend",
				 "We sent a response to the client: "
			    + responds 
			    , "frontend");
	}

	@Override
	public void playerSignIn(String userName, String password,
			String iPAddress, BooleanHolder result, StringHolder status) {
		
		CommonTools.writeLog("frontend", "frontend",
			 "start new call for signIn  username: "
		    + userName 
		    , "frontend");
		
		String message = buildSignInMsg(userName, password, iPAddress);		

		String responds = uDPTools_1.sendReceiveUDP(Integer.parseInt(CommonTools.getproperty("frontend.properties","UDPPort_" + "replica_0")),
				CommonTools.getproperty("frontend.properties", "serverName_" + "replica_0"),
				"replica_0",
				"frontend", 
				message);
						
		CommonTools.writeLog("frontend", "frontend",
			 "We got a response from the replica_0: "
		   + responds 
		   , "frontend");
					
		if(responds.equals("true")) {
		result.value = true; 
		status.value = "player sign in successeded";
		} else{
		result.value = false; 
		status.value = responds;
		}
		CommonTools.writeLog("frontend", "frontend",
			 "We sent a response to the client: "
		   + responds 
		   , "frontend");
	}

	@Override
	public void playerSignOut(String userName, String iPAddress,
			BooleanHolder result, StringHolder status) {
		
		CommonTools.writeLog("frontend", "frontend",
			 "start new call for signOut  username: "
		    + userName 
		    , "frontend");
		
		String message = buildSignOutMsg(userName, iPAddress);		

		String responds = uDPTools_1.sendReceiveUDP(Integer.parseInt(CommonTools.getproperty("frontend.properties","UDPPort_" + "replica_0")),
				CommonTools.getproperty("frontend.properties", "serverName_" + "replica_0"),
				"replica_0",
				"frontend", 
				message);
						
		CommonTools.writeLog("frontend", "frontend",
			 "We got a response from the replica_0: "
		   + responds 
		   , "frontend");
					
		if(responds.equals("true")) {
		result.value = true; 
		status.value = "player sign out successeded";
		} else{
		result.value = false; 
		status.value = responds;
		}
		CommonTools.writeLog("frontend", "frontend",
			 "We sent a response to the client: "
		   + responds 
		   , "frontend");
	}

	@Override
	public void transferAccount(String userName, String password,
			String OldiPAddress, String NewiPAddress, BooleanHolder result,
			StringHolder status) {
			
		CommonTools.writeLog("frontend", "frontend",
			 "start new call for transferAccount  username: "
		    + userName 
		    , "frontend");
		
		String message = buildTransferAccountMsg(userName, password, OldiPAddress, NewiPAddress);		

		String responds = uDPTools_1.sendReceiveUDP(Integer.parseInt(CommonTools.getproperty("frontend.properties","UDPPort_" + "replica_0")),
													CommonTools.getproperty("frontend.properties", "serverName_" + "replica_0"),
													"replica_0",
													"frontend", 
													message);
						
		CommonTools.writeLog("frontend", "frontend",
			 "We got a response from the replica_0: "
		   + responds 
		   , "frontend");
					
		if(responds.equals("true")) {
		result.value = true; 
		status.value = "player transfer successeded";
		} else{
		result.value = false; 
		status.value = responds;
		}
		CommonTools.writeLog("frontend", "frontend",
			 "We sent a response to the client: "
		   + responds 
		   , "frontend");
	
	}

	@Override
	public void getPlayerStatus(String adminUsername, String adminPassword,
			String iPAddress, BooleanHolder result, StringHolder playersStatus) {
		// TODO Auto-generated method stub
		CommonTools.writeLog("frontend", "frontend",
				 "start new call for get player status   adminUsername: "
			    + adminUsername 
			    , "frontend");
			
		String message = buildGetPlayerStatusMsg(adminUsername, adminPassword, iPAddress);		

		String responds = uDPTools_1.sendReceiveUDP(Integer.parseInt(CommonTools.getproperty("frontend.properties","UDPPort_" + "replica_0")),
				CommonTools.getproperty("frontend.properties", "serverName_" + "replica_0"),
				"replica_0",
				"frontend", 
				message);
						
		CommonTools.writeLog("frontend", "frontend",
			 "We got a response from the replica_0: "
		   + responds 
		   , "frontend");
		
		result.value = true;
		playersStatus.value = responds;
		
		CommonTools.writeLog("frontend", "frontend",
			 "We sent a response to the client: "
		   + responds 
		   , "frontend");
	}

	@Override
	public void suspendAccount(String adminUsername, String adminPassword,
			String iPAddress, String UsernameToSuspend, BooleanHolder result,
			StringHolder status) {
		
		CommonTools.writeLog("frontend", "frontend",
				 "start new call for suspend account   adminUsername: "
			    + adminUsername 
			    , "frontend");
			
		String message = buildSuspendMsg(adminUsername, adminPassword, iPAddress,UsernameToSuspend);		

		String responds = uDPTools_1.sendReceiveUDP(Integer.parseInt(CommonTools.getproperty("frontend.properties","UDPPort_" + "replica_0")),
				CommonTools.getproperty("frontend.properties", "serverName_" + "replica_0"),
				"replica_0",
				"frontend", 
				message);
						
		CommonTools.writeLog("frontend", "frontend",
			 "We got a response from the leader: "
		   + responds 
		   , "frontend");
					
		if(responds.equals("true")) {
		result.value = true; 
		status.value = "Admin suspend account successeded";
		} else{
		result.value = false; 
		status.value = responds;
		}
		CommonTools.writeLog("frontend", "frontend",
			 "We sent a response to the client: "
		   + responds 
		   , "frontend");

	}

	public String buildCreatePlayerAccountMsg(String firstName, String lastName, int age, String username, String password, String iPAddress) {
		
		String message = null;		
		message = Marshall.putTag(message, "Service", "player");
		message = Marshall.putTag(message, "Method", "createAccount");		
		message = Marshall.putTag(message, "FirstName", firstName);
		message = Marshall.putTag(message, "LastName", lastName);
		message = Marshall.putTag(message, "Age",  Integer.toString(age));
		message = Marshall.putTag(message, "UserName", username);
		message = Marshall.putTag(message, "Password", password);
		message = Marshall.putTag(message, "PlayerIpAddress", iPAddress);
		return message;
	}

	
	public String buildSignInMsg(String username, String password, String iPAddress) {
		
		String message = null;		
		message = Marshall.putTag(message, "Service", "player");
		message = Marshall.putTag(message, "Method", "signIn");		
		message = Marshall.putTag(message, "UserName", username);
		message = Marshall.putTag(message, "Password", password);
		message = Marshall.putTag(message, "PlayerIpAddress", iPAddress);
		return message;
	}
	
	public String buildSignOutMsg(String username,String iPAddress) {
		
		String message = null;		
		message = Marshall.putTag(message, "Service", "player");
		message = Marshall.putTag(message, "Method", "signOut");		
		message = Marshall.putTag(message, "UserName", username);
		message = Marshall.putTag(message, "PlayerIpAddress", iPAddress);
		return message;
	}
	
	public String buildTransferAccountMsg(String username, String password, String OldiPAddress, String NewiPAddress) {
		
		String message = null;		
		message = Marshall.putTag(message, "Service", "player");
		message = Marshall.putTag(message, "Method", "transfer");	
		message = Marshall.putTag(message, "UserName", username);
		message = Marshall.putTag(message, "Password", password);
		message = Marshall.putTag(message, "PlayerIpAddress", OldiPAddress);
		message = Marshall.putTag(message, "NewPlayerIpAddress", NewiPAddress);
		return message;
	}
	
	public String buildGetPlayerStatusMsg(String adminUsername, String adminPassword,String iPAddress) {
		
		String message = null;		
		message = Marshall.putTag(message, "Service", "admin");
		message = Marshall.putTag(message, "Method", "playerStatus");	
		message = Marshall.putTag(message, "AdminName", adminUsername);
		message = Marshall.putTag(message, "AdminPassword", adminPassword);
		message = Marshall.putTag(message, "AdminIpAddress", iPAddress);
		return message;
	}
	
	public String buildSuspendMsg(String adminUsername, String adminPassword,String iPAddress, String UsernameToSuspend) {
		
		String message = null;		
		message = Marshall.putTag(message, "Service", "admin");
		message = Marshall.putTag(message, "Method", "suspendAccount");	
		message = Marshall.putTag(message, "AdminName", adminUsername);
		message = Marshall.putTag(message, "AdminPassword", adminPassword);
		message = Marshall.putTag(message, "AdminIpAddress", iPAddress);
		message = Marshall.putTag(message, "UserNameToSuspend", UsernameToSuspend);
		return message;
	}
	
}

